package testedge

import (
	"fmt"
)


var (
	dropPackage int
	totalPackage int
	dropPackageRate float64
)

func ConculateRateInfo(size int) string {
	var s string
	g := float32(size / 1024.0 / 1024 / 1024)
	m := float32(size / 1024.0 / 1024)
	k := float32(size / 1024.0)
	b := float32(size)

	if g > 1 {
		s = fmt.Sprintf("%.1f G", g)
		return s
	} else if m > 1 {
		s = fmt.Sprintf("%.1f M", m)
		return s
	} else if k > 1 {
		s = fmt.Sprintf("%.1f K", k)
		return s
	} else {
		s = fmt.Sprintf("%.1f ", b)
		return s
	}
}

/*
对统计信息进行打印
*/
func PrintConclulateInfo(dir string, diffTime float64)  {
	//打印统计信息
	printCountInfo()
	//仅计算双向传输时速率
	if dir == HOLE {

		dropPackage = ReadFailCount + ReadTimeOutCount
		totalPackage = TestOk
		dropPackageRate = float64(dropPackage / totalPackage)
		totalCh := ConculateRateInfo(*CliTotal)
		speedCh := ConculateRateInfo(*CliTotal / int(diffTime) * 8)

		fmt.Printf("[SUM] %d sec, %sBytes %sBits/s totalPackages %d drops %d DPR %.5f\n",
			int(diffTime), totalCh, speedCh, totalPackage, dropPackage, dropPackageRate * 100)

	} else {

		dropPackage = ReadFailCount + ReadTimeOutCount
		totalPackage = TestOk
		if totalPackage == 0 {
			fmt.Printf("[SUM] %d sec, totalPackages %d drops %d DPR %.5f\n",
				int(diffTime), totalPackage, dropPackage, dropPackageRate * 100)
			return
		}
		dropPackageRate = float64(dropPackage / totalPackage)
		totalCh := ConculateRateInfo(*CliTotal)
		speedCh := ConculateRateInfo(*CliTotal / int(diffTime) * 8)

		fmt.Printf("[SUM] %d sec, %sBytes %sBits/s totalPackages %d drops %d DPR %.5f\n",
			int(diffTime), totalCh, speedCh, totalPackage, dropPackage, dropPackageRate * 100)

	}
}

func printCountInfo()  {
	fmt.Println("has creat connect number is :", ConnectCount)
	fmt.Println("read_failed number is :", ReadFailCount)
	fmt.Println("socket_failed number is :", DialFailCount)
	fmt.Println("write_failed number is :", WriteFailCount)
	fmt.Println("readtimeout_failed number is :", ReadTimeOutCount)
	fmt.Println("writetimeout_failed number is :", WriteTimeOutCount)
	fmt.Println("test_ok number is :", TestOk)
}
